﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using System.Collections.Generic;
using DBAccess;

namespace SD.Database
{
    public partial class Planets
    {
        /// <summary>
        /// Preform check if planet is empty and have proper fleetOwnerId in case it was sieged and siege was removed.
        /// </summary>
        /// <param name="fromSys"></param>
        /// <param name="fromPlanet"></param>
        public static void CheckPlanet(int sys, int planet)
        {
            int ownerId = Planets.PlanetOwner(sys, planet);
            int fleetOwner = Planets.FleetOnPlanetOwner(sys, planet);
            if (fleetOwner == ownerId || fleetOwner == 0) return;
            Dictionary<string, int> fleet = Planets.FleetOnPlanet(sys, planet);
            if (fleet["ColonyShips"] == 0 && fleet["Transports"] == 0 && fleet["Destroyers"] == 0 && fleet["Crusiers"] == 0 && fleet["Battleships"] == 0)
            {//sieged planet is now free
                DAL.GetInstance().UpdateData("Update planets SET FleetOwnerID = @param0 Where PlanetID = @param1 and SystemId = @param2", new List<object>() { ownerId, planet, sys });
            }
        }

        public static int PlanetOwner(int systemId, int planetId)
        {
            return DAL.GetInstance().GetInt("SELECT OwnerID FROM Planets Where PlanetID = @param0 and SystemId = @param1", new List<object>() { planetId, systemId });
        }
        public static int FleetOnPlanetOwner(int systemId, int planetId)
        {
            return DAL.GetInstance().GetInt("SELECT FleetOwnerID FROM Planets Where PlanetID = @param0 and SystemId = @param1", new List<object>() { planetId, systemId });
        }

        public static bool IsEmptyPlanet(int systemId, int planetId)
        {
            if (PlanetOwner(systemId, planetId) == 0) return true;
            else return false;
        }
        public static bool IsUnknown(int systemId, int planetId)
        {
            if (PlanetOwner(systemId, planetId) == 1) return true;
            else return false;
        }
        public static int PopulationOnPlanet(int sId, int pId)
        {
            return DAL.GetInstance().GetInt("SELECT PopulationLevel FROM Planets Where PlanetID = @param0 and SystemId = @param1", new List<object>() { pId, sId });
        }
        public static Dictionary<string, int> FleetOnPlanet(int sId, int pId)
        {
            Dictionary<string, int> tmp = new Dictionary<string, int>();
            DataSet ds = DAL.GetInstance().GetData("SELECT * FROM Planets Where PlanetID = @param0 and SystemId = @param1", new List<object>() { pId, sId });
            if(ds.Tables.Count > 0)
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    tmp.Add("ColonyShips", (int)double.Parse(row["ColonyShips"].ToString()));
                    tmp.Add("Transports", (int)double.Parse(row["Transports"].ToString()));
                    tmp.Add("Destroyers", (int)double.Parse(row["Destroyers"].ToString()));
                    tmp.Add("Crusiers", (int)double.Parse(row["Crusiers"].ToString()));
                    tmp.Add("Battleships", (int)double.Parse(row["Battleships"].ToString()));
                    tmp.Add("Starbase", (int)double.Parse(row["Starbase"].ToString()));
                }
            return tmp;
        }
    };
}